package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/login")
public class LoginContoller {
    @Autowired
    private EmpService empService;

    @PostMapping
    public Result login(@RequestBody Emp emp) {

        //调用业务层：登录功能
        Emp loginEmp = empService.login(emp);

      if (loginEmp != null){
          Map<String,Object>claime =new HashMap<>();
          claime.put("id",loginEmp.getId());
          String token =JwtUtils.generateJwt(claime);
          return Result.success(token);
      }
      return Result.error("用户名或密码错误");
    }
}

